package com.bos.service.impl;

import com.bos.dao.repositories.CourierRepository;
import com.bos.dao.repositories.FixedAreaRepository;
import com.bos.dao.repositories.TakeTimeRepository;
import com.bos.domian.base.Area;
import com.bos.domian.base.Courier;
import com.bos.domian.base.FixedArea;
import com.bos.domian.base.TakeTime;
import com.bos.service.FixedAreaSevice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.Map;

/**
 * @author misterWei
 * @create 2018年11月14号:13点31分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaSevice {
   @Autowired
   private FixedAreaRepository fixedAreaRepository;

    @Override
    public void save(FixedArea model) {
        fixedAreaRepository.save(model);
    }

    @Override
    public Map<String, Object> pageQuery(Integer page, Integer rows, FixedArea model) {
       Map<String, Object> fixedMap = new HashMap<String, Object>();
        Page<FixedArea> all = null;
        if (model!=null) {

            Specification<FixedArea> specification = new Specification<FixedArea>() {
                @Override
                public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Predicate and = null;
                    if (StringUtils.isNotBlank(model.getId())) {
                        Predicate query1 = cb.equal(root.get("id"), model.getId());
                          and = cb.and(query1);
                    }
                    if (StringUtils.isNotBlank(model.getCompany())) {
                        Predicate query2 = cb.like(root.get("company"), "%"+model.getCompany()+"%");
                          and = cb.and(query2);
                    }
                    if (StringUtils.isNotBlank(model.getFixedAreaName())) {
                        Predicate query3 = cb.like(root.get("fixedAreaName"), "%"+model.getFixedAreaName()+"%");
                          and = cb.and(query3);
                    }


                    return and;
                }
            };
            all = fixedAreaRepository.findAll(specification,new PageRequest(page, rows));
        }else {
            all = fixedAreaRepository.findAll(new PageRequest(page, rows));
        }
        fixedMap.put("total",all.getTotalElements());
        fixedMap.put("rows",all.getContent());
        return fixedMap;
    }
   //进行多表关联操作
    //关系分析
      //  多个分区 对应 多个快递员       外键由分区维护  多对多操作
     //   多个快递员 只能对应一个 时间    外键由快递员维护 多对一操作
    @Autowired
    private CourierRepository courierRepository;
    @Autowired
    private TakeTimeRepository takeTimeRepository;

    @Override
    public void associationCourierToFixedArea(String fixedAreaId, Integer courierId, Integer takeTimeId) {
      //使用持久态特性完成
        FixedArea fixedArea = fixedAreaRepository.getOne(fixedAreaId);
        Courier courier = courierRepository.findOne(courierId);
        TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
        fixedArea.getCouriers().add(courier);
        courier.setTakeTime(takeTime);


    }
}
